//
//  ChartViewController.swift
//  elarc01
//
//  Created by elarc on 16/6/17.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit
import PNChartSwift

class ChartViewController: UIViewController,PNChartDelegate{

    let ChartLabel = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        let items=["lineChart","barChart"] as [AnyObject]
        
        let segmented=UISegmentedControl(items:items)
        segmented.frame = CGRectMake(0,64,ScreenWidth,40)
        segmented.addTarget(self, action: #selector(ChartViewController.segmentDidchange),
                            forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(segmented)
        
        ChartLabel.frame = CGRectMake(0, 110, 320.0, 30)
        ChartLabel.textColor = PNGreenColor
        ChartLabel.font = UIFont(name: "Avenir-Medium", size:23.0)
        ChartLabel.textAlignment = NSTextAlignment.Center
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func segmentDidchange(segmented:UISegmentedControl){
 
        switch segmented.selectedSegmentIndex {
        case 0:
            //Add LineChart
            ChartLabel.text = "Line Chart"

            
            let lineChart:PNLineChart = PNLineChart(frame: CGRectMake(20, 150.0, 320, 200.0))
            lineChart.yLabelFormat = "%1.1f"
            lineChart.showLabel = true
            lineChart.backgroundColor = UIColor.clearColor()
            lineChart.xLabels = ["SEP 1","SEP 2","SEP 3","SEP 4","SEP 5","SEP 6","SEP 7"]
            lineChart.showCoordinateAxis = true
            lineChart.delegate = self
            
            // Line Chart Nr.1
            var data01Array: [CGFloat] = [60.1, 160.1, 126.4, 262.2, 186.2, 127.2, 176.2]
            let data01:PNLineChartData = PNLineChartData()
            data01.color = PNGreenColor
            data01.itemCount = data01Array.count
            data01.inflexionPointStyle = PNLineChartData.PNLineChartPointStyle.PNLineChartPointStyleCycle
            data01.getData = ({(index: Int) -> PNLineChartDataItem in
                let yValue:CGFloat = data01Array[index]
                let item = PNLineChartDataItem(y: yValue)
                return item
            })
            
            lineChart.chartData = [data01]
            lineChart.strokeChart()
            
            //        lineChart.delegate = self
            
            view.addSubview(lineChart)
            view.addSubview(ChartLabel)
            self.title = "Line Chart"

        case 1:
            //Add BarChart
            ChartLabel.text = "Bar Chart"
            
            let barChart = PNBarChart(frame: CGRectMake(20, 350.0, 320.0, 200.0))
            barChart.backgroundColor = UIColor.clearColor()
            //            barChart.yLabelFormatter = ({(yValue: CGFloat) -> NSString in
            //                var yValueParsed:CGFloat = yValue
            //                var labelText:NSString = NSString(format:"%1.f",yValueParsed)
            //                return labelText;
            //            })
            
            
            // remove for default animation (all bars animate at once)
            barChart.animationType = .Waterfall
            
            
            barChart.labelMarginTop = 5.0
            barChart.xLabels = ["SEP 1","SEP 2","SEP 3","SEP 4","SEP 5","SEP 6","SEP 7"]
            barChart.yValues = [1,24,12,18,30,10,21]
            barChart.strokeChart()
            
            barChart.delegate = self
            
            view.addSubview(ChartLabel)
            view.addSubview(barChart)
            
            self.title = "Bar Chart"

        default:
           print("Hello Chart")
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    func userClickedOnLineKeyPoint(point: CGPoint, lineIndex: Int, keyPointIndex: Int)
    {
        print("Click Key on line \(point.x), \(point.y) line index is \(lineIndex) and point index is \(keyPointIndex)")
    }
    
    func userClickedOnLinePoint(point: CGPoint, lineIndex: Int)
    {
        print("Click Key on line \(point.x), \(point.y) line index is \(lineIndex)")
    }
    
    func userClickedOnBarChartIndex(barIndex: Int)
    {
        print("Click  on bar \(barIndex)")
    }
}
